﻿package fab.buttons
{	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import caurina.transitions.Tweener;
	import caurina.transitions.properties.DisplayShortcuts;

	public class BoutonTimeline extends Sprite
	{
		public var activated:Boolean = false;
		public var clicable:Boolean = true;
		public var clicked_frame:int = 10;
		public var rollover_frame:int = 10;
		
		private var targetFrame:int = 0;
		private var btn:MovieClip;
		
		public function BoutonTimeline( btn:MovieClip)
		{
			//DisplayShortcuts.init();
/*
 * import caurina.transitions.Tweener;
import caurina.transitions.properties.DisplayShortcuts;
DisplayShortcuts.init();


stage.scaleMode = "noScale";

function rollover(ev:MouseEvent = null):void
{
	Tweener.addTween(anim, { _frame:anim.totalFrames, time:anim.totalFrames / 30, transition:"linear" });
}
function rollout(ev:MouseEvent = null):void
{
	Tweener.addTween(anim, { _frame:1, time:anim.totalFrames / 30, transition:"linear" });
}
anim.stop();
anim.addEventListener(MouseEvent.ROLL_OVER, rollover);
anim.addEventListener(MouseEvent.ROLL_OUT, rollout);
anim.buttonMode = true;
anim.mouseChildren = false;

*/
			this.btn = btn;
			addChild( btn );
			btn.gotoAndStop( 1 );
			buttonMode = true;
			
			btn.mouseChildren = false;
			
			btn.addEventListener(MouseEvent.MOUSE_OVER, _survole );
		}
		private function _framing( event:Event ):void
		{
			var btn:MovieClip = ( event.currentTarget as MovieClip );
			if ( targetFrame == 0 ) return;
			if ( btn.currentFrame == targetFrame ) btn.removeEventListener( Event.ENTER_FRAME, _framing );
			if ( btn.currentFrame > targetFrame ) btn.prevFrame();
			if ( btn.currentFrame < targetFrame ) btn.nextFrame();
		}	
		private function _survole(event:MouseEvent):void
		{
			if ( activated ) return;
			targetFrame = rollover_frame;
			btn.addEventListener( Event.ENTER_FRAME, _framing );
			btn.removeEventListener(MouseEvent.MOUSE_OVER, _survole );
			btn.addEventListener(MouseEvent.MOUSE_OUT, _sort );
		}
		private function _sort(event:MouseEvent):void
		{
			targetFrame = activated?clicked_frame:1;
			btn.addEventListener( Event.ENTER_FRAME, _framing );
			btn.addEventListener(MouseEvent.MOUSE_OVER, _survole );
			btn.removeEventListener(MouseEvent.MOUSE_OUT, _sort );
		}
		private function _relache(event:MouseEvent):void
		{
			btn.gotoAndStop( activated?clicked_frame:1 );
			btn.removeEventListener(MouseEvent.MOUSE_UP, _relache );
		}
	}
}